package com.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.bean.EmployeeBean;
import com.project.mapper.IEmployMapper;
import com.project.service.IEmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
@Service
@Transactional
public class EmployeeServiceImpl implements IEmployeeService {
    @Autowired
    IEmployMapper employMapper;

    @Override
    public void add(EmployeeBean employee) {
        employMapper.insert(employee);
    }

    @Override
    public void dimission(Integer id) {
        employMapper.deleteById(id);
    }

    @Override
    public void update(Integer id, String job) {
        EmployeeBean employeeBean = employMapper.selectById(id);
        employeeBean.setJob(job);
        employMapper.updateById(employeeBean);
    }

    @Override
    public IPage<EmployeeBean> findByItem(Integer pageNO, String name, LocalDate startDate, LocalDate endDate) {
        QueryWrapper<EmployeeBean> qw = new QueryWrapper<>();
        if (name != null && name.length() != 0){
            qw.like("e_name",name);
        }
        if (startDate != null){
            qw.ge("e_birthday",startDate);
        }
        if (endDate != null){
            qw.le("e_birthday",endDate);
        }
        return employMapper.selectPage(new Page<>(pageNO,3),qw);
    }

    @Override
    public EmployeeBean findById(Integer id) {
        return employMapper.selectById(id);
    }
}
